/*
 * @Description:
 * @Autor: fage
 * @Date: 2022-06-06 13:52:09
 * @LastEditors: fage
 * @LastEditTime: 2022-07-07 09:50:38
 */
const fs = require("fs");
function timestr(fmt = "MM-dd hh:mm:ss", now = new Date()) {
  var o = {
    "Y+": now.getFullYear(), //月份
    "M+": now.getMonth() + 1, //月份
    "d+": now.getDate(), //日
    "h+": now.getHours(), //小时
    "m+": now.getMinutes(), //分
    "s+": now.getSeconds(), //秒
    "q+": Math.floor((now.getMonth() + 3) / 3), //季度
    S: now.getMilliseconds(), //毫秒
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(
      RegExp.$1,
      (now.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
      );
  return fmt;
}
function log(...msg) {
  msg.unshift("\n\n【" + timestr() + "】\n");
  console.log(...msg);
}
function obj2arr(obj) {
  let arr = [];
  if (!Array.isArray(obj)) {
    for (key in obj) {
      arr.push(obj[key]);
    }
  } else {
    arr = obj;
  }
  return arr;
}
function getDir(dir) {
  const files = fs.readdirSync(dir);
  const dirs = [];
  for (let f in files) {
    const ff = fs.statSync(dir + files[f]);
    if (ff.isDirectory()) {
      dirs.push(dir + files[f]);
    }
  }
  return dirs;
}
module.exports = { timestr, log, obj2arr, getDir };
